//
//  SearchResultVM.swift
//  LeTu
//
//  Created by g1 on 2024/1/14.
//

import Foundation

class SearchResultVM {
    
    /// 关键词
    var keyword: String
    /// 收藏数据
    private(set) var sourceArr = [ScenicModel]()
    
    init(keyword: String) {
        self.keyword = keyword
    }
    
    // MARK: - 获取城市列表
    func getCitys(isLoad: Bool, completeHandle: @escaping NetworkRequestHandle) {

        var param: [String : Any] = [String : Any]()
        param["keyword"] = keyword
        
        apiProvider.request(api: .searchScenic(params: param)) { [weak self] json, jsonArr, code in
            MYLog("城市列表：", json)
                        
            var dialog = DialogMessage()

            guard let strongSelf = self, let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard dialog.code == 200, let dataDic = response.data as? [[String: Any]] else { return completeHandle(dialog) }
            
            let tempList = Mapper<ScenicModel>().mapArray(JSONArray: dataDic)
            if isLoad {
                strongSelf.sourceArr = strongSelf.sourceArr + tempList
            } else {
                strongSelf.sourceArr = tempList
            }
            
            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
        
    }
    
}
